<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>

    </style>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>
                <input type="button" value="添加" class="btn btn-primary" @click="add()">
            </div>
        </div>
        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime}}</td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                id: '',
                name: '',
                list: [{
                        id: 1,
                        name: "奔驰",
                        ctime: new Date()
                    },
                    {
                        id: 2,
                        name: "宝马",
                        ctime: new Date()
                    }
                ]
            },
            methods: {
                add() {
                    var car = {
                        id: this.id,
                        name: this.name,
                        ctime: new Date()
                    }
                    this.list.push(car)
                    this.id = this.name = ''
                },
                del(id) { //根据 id 删除数据
                    //分析
                    //1. 如何根据 id 找到找到要删除这一项的索引
                    //2. 如果找到索引了,直接调用数组的 splice 方法

                    // this.list.some((item, i)=> {
                    //     if(item.id == id) {
                    //         this.list.splice(i,1)
                    //         //在数组的 some 方法中,如果 return true ,就会立刻这个数组的后续循环
                    //         return true;
                    //     }
                    // })

                    var index = this.list.findIndex(item => {
                        if (item.id == id) {
                            return true;
                        }
                    })
                    // console.log(index)
                    this.list.splice(index, 1)
                    
                }
            }
        })
    </script>

</body>

</html>